#!/usr/bin/make -f

pkg_name = temurin-8-jre
priority = 1082
# The list below must be kept in sync with the jinfo.in file
jvm_tools = java jjs keytool orbd pack200 policytool rmid rmiregistry servertool tnameserv unpack200
amd64_tarball_url = https://github.com/adoptium/temurin8-binaries/releases/download/jdk8u392-b08/OpenJDK8U-jre_x64_linux_hotspot_8u392b08.tar.gz
amd64_checksum = 91d31027da0d985be3549714389593d9e0da3da5057d87e3831c7c538b9a2a0f
arm64_tarball_url = https://github.com/adoptium/temurin8-binaries/releases/download/jdk8u392-b08/OpenJDK8U-jre_aarch64_linux_hotspot_8u392b08.tar.gz
arm64_checksum =37b997f12cd572da979283fccafec9ba903041a209605b50fcb46cc34f1a9917
armhf_tarball_url = https://github.com/adoptium/temurin8-binaries/releases/download/jdk8u392-b08/OpenJDK8U-jre_arm_linux_hotspot_8u392b08.tar.gz
armhf_checksum = 877953bfabcdbcd000c11364d806456ca579a921085de2ca942280ebe168cac2
ppc64el_tarball_url = https://github.com/adoptium/temurin8-binaries/releases/download/jdk8u392-b08/OpenJDK8U-jre_ppc64le_linux_hotspot_8u392b08.tar.gz
ppc64el_checksum = 0ecb0aeb54fb9d3c9e1a7ea411490127e8e298d93219fafc4dd6051a5b74671f

d = debian/$(pkg_name)
jvm_home = usr/lib/jvm
jvm_dir = $(pkg_name)-$(DEB_HOST_ARCH)
pkg_alias = $(pkg_name)-$(DEB_HOST_ARCH)

%:
	dh $@

override_dh_auto_clean:
override_dh_auto_test:
override_dh_auto_build:
override_dh_strip:
override_dh_auto_install:
	# Download and verify checksum of JRE
	wget --progress=dot:mega -O jre.tar.gz $($(DEB_HOST_ARCH)_tarball_url)
	echo '$($(DEB_HOST_ARCH)_checksum) jre.tar.gz' > jre.tar.gz.sha256.txt
	sha256sum -c jre.tar.gz.sha256.txt

	# Substitute variables.
	sed \
		-e 's/@pkg_alias@/$(pkg_alias)/g' \
		-e 's/@jvm_dir@/$(jvm_dir)/g' \
		-e 's/@priority@/$(priority)/g' \
		-e 's/@jvm_tools@/$(jvm_tools)/g' \
	    debian/jinfo.in > debian/jinfo

	sed \
		-e 's/@pkg_alias@/$(pkg_alias)/g' \
		-e 's/@jvm_dir@/$(jvm_dir)/g' \
		-e 's/@priority@/$(priority)/g' \
		-e 's/@jvm_tools@/$(jvm_tools)/g' \
	    debian/postinst.in > debian/postinst

	sed \
		-e 's/@pkg_alias@/$(pkg_alias)/g' \
		-e 's/@jvm_dir@/$(jvm_dir)/g' \
		-e 's/@priority@/$(priority)/g' \
		-e 's/@jvm_tools@/$(jvm_tools)/g' \
	    debian/prerm.in > debian/prerm

	# Unpack JRE.
	dh_installdirs \
		$(jvm_home) \
		$(jvm_home)/$(jvm_dir)
	tar --strip-components=1 -C "$(d)/$(jvm_home)/$(jvm_dir)" -xf jre.tar.gz

	# Add jinfo file (alternatives support).
	cp debian/jinfo "$(d)/$(jvm_home)/.$(pkg_alias).jinfo"

	# Replace bundled cacerts and redirect to adoptium-ca-certificates.
	rm -f "$(d)/$(jvm_home)/$(jvm_dir)/lib/security/cacerts"
	ln -s /etc/ssl/certs/adoptium/cacerts "$(d)/$(jvm_home)/$(jvm_dir)/lib/security/cacerts"

override_dh_gencontrol:
	dh_gencontrol --
